<?php
namespace app\admin\logic;
use app\admin\validate\Region as RegionValidate;

class RegionLogic extends BaseLogic {

    /**
     * 获取区域查询条件
     * @param $branchid
     * @param $floorid
     * @return array
     */
    public function get_search_region_maps($branchid,$floorid){
        $maps = [];
        if($branchid != -1){
            $f_maps = [];
            $f_maps[] = ['branchid','=',$branchid];
            $floorids = model('floor')->get_column($f_maps,'id');
            $maps[] = ['floorid','in',$floorids];
        }

        if($floorid != -1){
            $maps[] = ['floorid','=',$floorid];
        }
        return $maps;
    }

    /**
     * 检查新增区域规则
     * @param $params
     * @return mixed
     */
    public function check_add_region_rule($params){
        $validate = new RegionValidate();
        $r = $validate->scene('add')->check($params);
        if(!$r){
            return $this->get_response(0,$validate->getError());
        }

        //$maps = [];
        //$maps[] = ['floorid','=',$params['floorid']];
        //$maps[] = ['name','=',trim($params['name'])];
        //$r_detail = model('region')->get_detail($maps,'id');
        //if(!empty($r_detail)){
            //return $this->get_response(0,'该区域已存在');
        //}
        return $this->get_response(1,'OK');
    }

    /**
     * 检查修改区域规则
     * @param $params
     * @return mixed
     */
    public function check_save_region_rule($params){
        $validate = new RegionValidate();
        $r = $validate->scene('edit')->check($params);
        if(!$r){
            return $this->get_response(0,$validate->getError());
        }

        $maps = [];
        $maps[] = ['id','=',$params['regionid']];
        $r_detail = model('region')->get_detail($maps,'id');
        if(empty($r_detail)){
            return $this->get_response(0,'该区域不存在');
        }

        $maps = [];
        $maps[] = ['id','<>',$params['regionid']];
        $maps[] = ['floorid','=',$params['floorid']];
        $maps[] = ['name','=',trim($params['name'])];
        $detail = model('region')->get_detail($maps,'id');
        if(!empty($detail)){
            return $this->get_response(0,'该区域已存在');
        }

        $data['r_detail'] = $r_detail;
        return $this->get_response(1,'OK',$data);
    }
}